package com.wanghua.spring.interceptor.struts2;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import java.util.Map;

public class AuthenticationInterceptor extends AbstractInterceptor {
    public AuthenticationInterceptor() {
        super();
    }

    @Override
    public void init() {
        super.init();
    }

    @Override
    public void destroy() {
        super.destroy();
    }

    //此代码会在Action被调用前运行。
    @Override
    public String intercept(ActionInvocation actionInvocation) throws Exception {
        ActionContext actionContext = actionInvocation.getInvocationContext();
        Map<String, Object> session = actionContext.getSession();

        String account = (String) session.get("account");
        if (account == null) {//session中不存在帐户
            return Action.LOGIN;
        } else {
            return actionInvocation.invoke();  //如果验证通过，调用此代码后会继续调用Action。
        }
    }
}
